package com.bpm.example.event.condition;

import com.bpm.common.util.FlowableEngineUtil;
import com.google.common.collect.ImmutableMap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.Test;

import java.util.Map;

/**
 * 条件启动事件
 */
@Slf4j
public class RunConditionalStartEventProcessDemo
        extends FlowableEngineUtil {
    @Test
    public void runConditionalStartEventProcessDemo() {
             //加载Flowable配置文件并初始化流程引擎及服务
        initFlowableEngineAndServices("flowable.cfg.xml");
        //部署流程
        ProcessDefinition processDefinition = deployResource("processes/conditional/ConditionalStartEventForEventSubProcess.bpmn20.xml");

        //发起流程
        ProcessInstance procInst = runtimeService
                .startProcessInstanceById(processDefinition.getId());
        log.info("流程实例id：{}", procInst.getId());
       TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        //查询第一个任务
        Task task1 = taskQuery.singleResult();
        log.info("第一个任务名称为: {}", task1.getName());
        //设置下条件的值
        Map<String, Object> variables = ImmutableMap.of("exceptionNum", 10);
        //在运行中的流程实例中手动触发条件事件（Conditional Event）判断和执行
        runtimeService.evaluateConditionalEvents(procInst.getId(), variables);
       //查询任务数量
        long count = taskQuery.count();
        log.info("当前待办任务数量为：{}", count);
    }
}
